package com.ptu.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ptu.constant.MessageConstant;
import com.ptu.constant.PasswordConstant;
import com.ptu.constant.StatusConstant;
import com.ptu.context.BaseContext;
import com.ptu.dto.StoreDTO;
import com.ptu.dto.UsersDTO;
import com.ptu.dto.UsersDTO;
import com.ptu.dto.UsersLoginDTO;
import com.ptu.entity.Banner;
import com.ptu.entity.Store;
import com.ptu.entity.Users;
import com.ptu.entity.Users;
import com.ptu.exception.AccountLockedException;
import com.ptu.exception.AccountNotFoundException;
import com.ptu.exception.PasswordErrorException;

import com.ptu.mapper.UsersMapper;
import com.ptu.service.UsersService;
import com.ptu.vo.StoreVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UsersServiceImpl implements UsersService {

    @Autowired
    private UsersMapper usersMapper;

    /**
     * 用户登录
     *
     * @param usersLoginDTO
     * @return
     */
    public Users login(UsersLoginDTO usersLoginDTO) {
        String username = usersLoginDTO.getUsername();
        String password = usersLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Users users = usersMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (users == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        //明文进行加密处理
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(users.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        //3、返回实体对象
        return users;
    }

    /**
     * 用户注册
     *
     * @param usersDTO
     */
    public void save(UsersDTO usersDTO) {
        System.out.println("当前线程id:" + Thread.currentThread().getId());
        String password = usersDTO.getPassword();
        Users users = new Users();
        //对象属性拷贝
        BeanUtils.copyProperties(usersDTO, users);
        users.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        users.setCreateTime(LocalDateTime.now());
        usersMapper.insert(users);
    }


    /**
     * 编辑员工信息
     * @param usersDTO
     */
    @Override
    public void update(UsersDTO usersDTO) {
        Users users = new Users();
        BeanUtils.copyProperties(usersDTO, users);
        if (users.getPassword() != null){
            users.setPassword(DigestUtils.md5DigestAsHex(users.getPassword().getBytes()));
        }
        usersMapper.update(users);

    }


    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @Override
    public Users getById(Long id) {
        Users users = usersMapper.getById(id);
        users.setPassword("****");
        return users;
    }
}
